package com.sdu.cloudservice.action;

import com.auth0.jwt.JWT;
import com.sdu.cloudservice.dto.Envelope;
import com.sdu.cloudservice.entity.UserEntity;
import com.sdu.cloudservice.service.AdminService;
import com.sdu.cloudservice.service.UserService;
import com.sdu.cloudservice.token.UserToken;
import com.sdu.cloudservice.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lenovo on 2019/6/24.
 * @author
 * ZMX 后来被李昊明改了
 */
@Controller
@CrossOrigin
@RequestMapping("/user")
public class UserAction {
    @Autowired
    private UserService userservice;
    @Autowired
    private AdminService adminService;

    @RequestMapping( value = "/login",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String login(String username,String password){
//        System.out.println(session.getId());
//        System.out.println(username);
//        System.out.println(password);
        Envelope envelope=userservice.login(username,password);
        if(envelope.getCode()!=0){
            return envelope.toString();
        }
        // 计算得到token
        String token = PasswordUtil.getToken((UserEntity) envelope.getObj());

        envelope.setObj("user");
        // 设置信封的token
        envelope.setToken(token);
        // JSONObject jsonObject = new JSONObject();
        // jsonObject.put("token",session.getId());
        // envelope.setObj(jsonObject);
        return envelope.toString();
    }
    @RequestMapping(value="/register",method = RequestMethod.POST)
    @ResponseBody
    public String register(String username,String password){
        Envelope envelope=userservice.register(username,password);
        if(envelope.getCode()!=0){
            return  envelope.toString();
        }
        // session.setAttribute("user",envelope.getObj());
        // 计算token
        String token = PasswordUtil.getToken((UserEntity) envelope.getObj());
//        envelope.setObj("user");
        // 设置token
        envelope.setToken(token);
        return envelope.toString();
    }

    @RequestMapping(value="/verifyPassword",method = RequestMethod.POST)
    @ResponseBody
    @UserToken
    public String verifyPassword(HttpServletRequest httpServletRequest, String password){
        // Object userEntity1=session.getAttribute("user");
        String token = httpServletRequest.getHeader("token");// 从 http 请求头中取出 token
//
//        if(userEntity1==null)
//            return new Envelope(11,"请先登录",null).toString();
//
//        UserEntity userEntity=(UserEntity)userEntity1;
//        String username=userEntity.getUsername();
        String username = JWT.decode(token).getAudience().get(0);
        Envelope envelope = null;
        // 如果是管理员验证密码
        if(JWT.decode(token).getClaim("identity").asString().equals("admin")){
            envelope = adminService.verify_password(username,password);
        }else{
            envelope = userservice.verify_password(username,password);
        }

//        System.out.println(username);
//        System.out.println(password);


        if(envelope.getCode()==0)//密码验证正确
            // 设置携带对象
            envelope.setObj(PasswordUtil.getMd5(username));
            //session.setAttribute("state",1);
        return envelope.toString();
    }

    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    @ResponseBody
    @UserToken
    public String changePassword(HttpServletRequest httpServletRequest,String verifyToken,String newPassword){
        // 从 http 请求头中取出 token
        String token = httpServletRequest.getHeader("token");
        // 从token还原用户名
        String username = JWT.decode(token).getAudience().get(0);
        // 如果验证不正确
        if(!verifyToken.equals(PasswordUtil.getMd5(username))){
            return new Envelope(13,"清先验证原密码",null).toString();
        }
        if(JWT.decode(token).getClaim("identity").asString().equals("admin")){
            return adminService.change_password(username,newPassword).toString();
        }else{
            return userservice.change_password(username,newPassword).toString();
        }
        //Envelope envelope=;
        //return envelope.toString();
//            Object S1=session.getAttribute("state");
//            if(S1==null){
//                System.out.println("null");
//                return new Envelope(13,"清先验证原密码",null).toString();
//            }
//            int S=(Integer) S1;
//            if(S==1){
//                Object userEntity1=session.getAttribute("user");
//                if(userEntity1==null)
//                    return new Envelope(12,"登录过期",null).toString();
//                UserEntity userEntity=(UserEntity)userEntity1;
//                String username=userEntity.getUsername();
//
//                session.setAttribute("user",envelope.getObj());
//                session.setAttribute("state",0);
//                envelope.setObj(null);
//                return envelope.toString();
//            }

//            return new Envelope(7,"用户名与密码不匹配，请重新输入密码",null).toString();
    }
    @RequestMapping(value = "/addDownload",method = RequestMethod.POST)
    @ResponseBody
    @UserToken
    public String addDownload(HttpServletRequest httpServletRequest,int code){
        // 从 http 请求头中取出 token
        String token = httpServletRequest.getHeader("token");
        String username;

//        username = "admin";
        // 如果是管理员
        if(PasswordUtil.getRole(token).equals("admin")){
            // 直接设置用户名为管理员
            username = "admin";
        }else{
            // 从token还原用户名
            username = JWT.decode(token).getAudience().get(0);
        }
        try{
            return userservice.addDownload(username,code).toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
    }
//    @RequestMapping("/info")
//    @ResponseBody
//    @UserToken
//    public String info(HttpSession session){
//
//        return new Envelope("欢迎您xxx").toString();
//    }



}
